<?php

namespace App\Http\Controllers\Api\V1\Goods;

use App\Models\Goods;
use App\Http\Controllers\Api\V1\CustomFommat;
use App\Http\Controllers\Api\V1\BaseController;
use App\Http\Controllers\Api\V1\TransformerController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class GoodsController extends BaseController
{
    /**
     * @var array 金币等级
     */
    public static $credit =[
        2 => [0, 100],
        3 => [100, 300],
        4 => [300, 800],
        5 => [800, 1600],
    ];
    /**
     * getGoodsList  获取商品列表
     *
     * @return \Dingo\Api\Http\Response
     */
    public function getGoodsList(){
        $lists = Goods::paginate(10);
        return $this->response->paginator($lists, new TransformerController,[], function ($resource, $fractal) {
            $fractal->setSerializer(new CustomFommat());
        });
    }

    /**
     * getItem 获取单个商品信息
     *
     * @param $id int 商品id
     * @return \Dingo\Api\Http\Response
     */
    public function getItem($id){
        if (!$id)
            return $this->response->error('参数错误.', 400);
        $list = Goods::findOrFail($id);
        if (!$list)
            return $this->response->errorInternal();
        return $this->response->item($list, new TransformerController, function ($resource, $fractal) {
            $fractal->setSerializer(new CustomFommat());
        });
    }

    /**
     * searchLists 根据条件查询商品结果
     *
     * @param null $type 根据金币范围搜素
     * @return \Dingo\Api\Http\Response
     */
    public function searchListsType($type = null){
        $lists = Goods::paginate(10);
        if($type){
            switch ($type){
                case 0:
                    $lists =Goods::where('price', '>', 0)->paginate(10);
                    break;
                case 1:
                    $user = auth()->user();
                    $lists =Goods::where('price', '=', $user->reward)->paginate(10);
                    break;
                case 6:
                    $lists =Goods::where('price', '>=', 1600)->paginate(10);
                    break;
            }
            if (in_array($type, array_keys(self::$credit))){
                $lists =Goods::whereBetween('price',self::$credit[$type])->paginate(10);
            }
            return $this->response->paginator($lists, new TransformerController, [], function ($resource, $fractal) {
                $fractal->setSerializer(new CustomFommat());
            });
        }
        return $this->response->paginator($lists, new TransformerController, [], function ($resource, $fractal) {
            $fractal->setSerializer(new CustomFommat());
        });
    }

    /**
     * searchLists 根据条件查询商品结果
     *
     * @param null $name 根据商品名称搜索
     * @return \Dingo\Api\Http\Response
     */
    public function searchListsName($name = null){
        $lists = Goods::paginate(10);
        if($name){
            $lists = Goods::where('name','like','%'.$name.'%')->paginate(10);
            return $this->response->paginator($lists, new TransformerController, [], function ($resource, $fractal) {
                $fractal->setSerializer(new CustomFommat());
            });
        }
        return $this->response->paginator($lists, new TransformerController, [], function ($resource, $fractal) {
            $fractal->setSerializer(new CustomFommat());
        });
    }

    /**
     * searchMergeLists 多条件商品搜索
     *
     * @param int $type 商品类别
     * @param null $name 商品名称
     *
     * @return \Dingo\Api\Http\Response
     */
    public function searchMergeLists($type = 0, $name = null, Request $request){
        $perpage = $request->perpage ? $request->perpage : 10;
        $lists = Goods::where('name','like','%'.$name.'%')
            ->when($type, function($query) use($type){

                switch ($type){
                    case 0:
                        $return = $query->where('price', '>', 0);
                        break;
                    case 1:
                        $user = auth()->user();
                        $return = $query->where('price', '=', $user->reward);
                        break;
                    case 6:
                        $return = $query->where('price', '>=', 1600);
                }
                if(in_array($type,array_keys(self::$credit))){
                    $return = $query->whereBetween('price',self::$credit[$type]);
                }
                return $return ;
        })->paginate($perpage);
        return $this->response->paginator($lists, new TransformerController, [], function ($resource, $fractal) {
            $fractal->setSerializer(new CustomFommat());
        });
    }
}
